package com.example.demo.config;

import com.example.demo.controller.data.ReturnData;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

//处理全局异常错误的类
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)//处理所有的异常
    public ResponseEntity<ReturnData> hand(Exception e){
        if(e instanceof IllegalStateException ){//如果是参数异常
            return ResponseEntity.ok(ReturnData.error("参数异常",e.toString()));
        }

        return ResponseEntity.ok(ReturnData.error("后台服务异常",e.toString()));
    }
}
